<template>

  <el-row>
    <el-col>
      <basic-container>
        <avue-crud :option="option"
                   :table-loading="loading"
                   :data="data"
                   :page="page"
                   :permission="permissionList"
                   :before-open="beforeOpen"
                   v-model="form"
                   ref="crud"
                   @row-update="rowUpdate"
                   @row-save="rowSave"
                   @row-del="rowDel"
                   @search-change="searchChange"
                   @search-reset="searchReset"
                   @selection-change="selectionChange"
                   @current-change="currentChange"
                   @size-change="sizeChange"
                   @on-load="onLoad">
          <template slot-scope="{type,size,row,index}" slot="menuBtn">
            <el-dropdown-item divided @click.native="showJobRecords(row,index)">审&nbsp;&nbsp;&nbsp;&nbsp;批
            </el-dropdown-item>
          </template>
        </avue-crud>
      </basic-container>
    </el-col>
    <el-dialog
      title="审批"
      :visible.sync="dialogVisible"
      width="30%"
      center
      :before-close="handleClose">
      <el-select v-model="isReply" width="100%" size="medium" placeholder="请选择" @change="selechange">
        <el-option
          v-for="item in approvers"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitrep">确 定</el-button>
  </span>
    </el-dialog>
  </el-row>
</template>

<script>
import {getList, getDetail, add, update, remove, ApprovalLeave} from "@/api/service/qjinit";
import {mapGetters} from "vuex";
// import {getStore} from "@/util/store";
// import {getBjTree} from "@/api/service/org";

export default {
  data() {
    return {
      id: "0", //请假id
      dialogVisible: false,
      treeBj: '0',
      isReply: 0,
      approvers: [
        {
        label: "未审批",
        value: 0
        },
        {
          label: "通过",
          value: 1
        },
        // {
        //   label: "未通过",
        //   value: 2
        // },
        // {
        //   label: "学生撤销",
        //   value: 3
        // },
        // {
        //   label: "家长撤销",
        //   value: 4
        // }
        ],
      treeData: [],
      treeOption: {
        nodeKey: 'id',
        lazy: false,
        addBtn: false,
        menu: false,
        size: 'small',
        defaultExpandAll: true,
        props: {
          labelText: '标题',
          label: 'title',
          value: 'value',
          children: 'children'
        }
      },
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        editBtn: false,
        menuType: 'menu',
        addBtn: false,
        menuBtnTitle: '更多操作',
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false
          },
          {
            label: "学生",
            search: true,
            prop: "studentName",
            rules: [{
              required: true,
              message: "请输入学生名称",
              trigger: "blur"
            }],
            slot: true,
            checkStrictly: true,
            editDisabled: true,
          },
          {
            label: "开始时间",
            prop: "startTime",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            rules: [{
              required: true,
              message: "请输入开始时间",
              trigger: "blur"
            }]
          },
          {
            label: "请假原因",
            prop: "content",
            type: "textarea",
            span: 24,
            minRows: 6,
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入请假原因",
              trigger: "blur"
            }],
            maxlength: 500,
            showWordLimit: true
          },
          // {
          //   label: "是否回复",
          //   prop: "isReply",
          //   rules: [{
          //     required: true,
          //     message: "请输入是否回复",
          //     trigger: "blur"
          //   }],
          //   type: "select",
          //   dicData: [
          //     {
          //       label: "未回复",
          //       value: 0
          //     },
          //     {
          //       label: "已回复",
          //       value: 1
          //     }
          //   ],
          //   hide: true,
          // },
          {
            label: "状态",
            prop: "isReply",
            rules: [{
              required: true,
              message: "请输入状态",
              trigger: "blur"
            }],
            type: "select",
            dicData: [
              {
                label: "未审批",
                value: 0
              },
              {
                label: "通过",
                value: 1
              },
              // {
              //   label: "未通过",
              //   value: 2
              // },
              // {
              //   label: "学生撤销",
              //   value: 3
              // },
              // {
              //   label: "家长撤销",
              //   value: 4
              // }
            ],
            // value:1,
            editDisabled: false,
            addDisabled: true
          },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission", "JSuserInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.qjinit_add, false),
        viewBtn: this.vaildData(this.permission.qjinit_view, false),
        delBtn: this.vaildData(this.permission.qjinit_delete, false),
        editBtn: this.vaildData(this.permission.qjinit_edit, false)
      };
    },
    js_Id() {
      return this.JSuserInfo.js_Id
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    // var userinfo = getStore({
    //   name: "userInfo",
    // });
    // getBjTree({jsId: userinfo.relateId}).then(res => {
    //   this.treeData = res.data.data;
    // });
  },
  methods: {
    nodeClick(data) {
      this.treeBj = data.id;
      this.onLoad(this.page);
    },
    rowSave(row, done, loading) {
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      console.log(this.form.id)
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id, this.form.xs_id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    //审批
    showJobRecords(row, index) {
      console.log(index)
      if (row.isReply == 0) {
        this.dialogVisible = true
        this.isReply = row.isReply
        this.id = row.id
      } else {
        this.$message({
          message: '该请假已经审批。',
          type: 'warning'
        });
      }
    },
    selechange(row) {
      this.isReply = row
    },
    submitrep() {
      let data = {
        id: this.id,
        // AppUserId: this.js_Id,
        types: this.isReply
      }
      ApprovalLeave(data).then(res => {
        console.log(data)
        if (res.data.code == 200) {
          this.$message({
            message: '审批成功',
            type: 'success'
          });
        } else {
          this.$message.error(res.data.msg);
        }
        this.dialogVisible = false
        this.onLoad(this.page)
      })
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      // params.AppUserId = this.js_Id + "";
      // params.TypeId = "2";
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        // this.option.column.forEach(item=>{
        //   if(item.prop == 'studentName'){
        //     item.search = true
        //   }
        // })
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style scoped>
/deep/ .el-select {
  width: 100%;
}
</style>
